using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;

namespace DAL
{
    public class DIEUKIENDAL
    {
        private SqlConnection cn = null;
        public DIEUKIENDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(DIEUKIEN dk)
        {
            SqlCommand cm = new SqlCommand("Dieu_kien_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@dieukien", dk.Dieukien);
            cm.Parameters.AddWithValue("@hansudung",dk.Hansudung);
            cm.Parameters.AddWithValue("@diachi", dk.Diachi);
            cm.Parameters.AddWithValue("@dienthoai",dk.Dienthoai);
            cm.Parameters.AddWithValue("@giomocua", dk.Giomocua);
            cm.ExecuteNonQuery();
        }
        public void Update(DIEUKIEN dk)
        {
            SqlCommand cm = new SqlCommand("Dieu_kien_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madkien", dk.Madkien);
            cm.Parameters.AddWithValue("@dieukien", dk.Dieukien);
            cm.Parameters.AddWithValue("@hansudung", dk.Hansudung);
            cm.Parameters.AddWithValue("@diachi", dk.Diachi);
            cm.Parameters.AddWithValue("@dienthoai", dk.Dienthoai);
            cm.Parameters.AddWithValue("@giomocua", dk.Giomocua);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("Dieu_kien_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madkien", iD);
            cm.ExecuteNonQuery();
        }
        public Dieu_kienCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("Luu_y_Get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            Dieu_kienCollection dieukienCollection = new Dieu_kienCollection();

            while (dr.Read()) 
            {
                DIEUKIEN dieukien = new DIEUKIEN();
                dieukien.Madkien = int.Parse(dr.GetValue(0).ToString());
                dieukien.Dieukien = dr.GetValue(1).ToString();
                dieukien.Hansudung = DateTime.Parse(dr.GetValue(2).ToString());
               dieukien.Diachi = dr.GetValue(3).ToString();
               dieukien.Dienthoai = dr.GetValue(4).ToString();
               dieukien.Giomocua = DateTime.Parse(dr.GetValue(5).ToString());
               dieukienCollection.Add(dieukien);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return dieukienCollection;
        }
    }
}
